package com.anolesoft.yr.mbus.fin.controller;

import com.anolesoft.common.core.web.domain.AjaxResult;
import com.anolesoft.yr.mbus.fin.dto.FinBillingApplyDTO;
import com.anolesoft.yr.mbus.fin.service.FinBillingApplyService;
import com.anolesoft.yr.mbus.fin.vo.FinBillingApplyVO;
import com.anolesoft.yr.util.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @author zhj
 * @date 2020/8/12 16:36
 */
@RestController
public class FinBillingApplyController {
    @Autowired
    private FinBillingApplyService finBillingApplyService;

    @PostMapping(value = "/api/fin/finbillingapply/add", produces = MediaType.APPLICATION_JSON_VALUE)
    public AjaxResult create(FinBillingApplyVO finBillingApplyVO) {
        FinBillingApplyDTO finBillingApplyDTO = vo2dto(finBillingApplyVO, true);
        return new AjaxResult(HttpStatus.SUCCESS, Constants.CREATE_SUCCESS_MESSAGE, finBillingApplyService.create(finBillingApplyDTO));
    }

    @PutMapping(value = "/api/fin/finbillingapply/update", produces = MediaType.APPLICATION_JSON_VALUE)
    public AjaxResult modify(FinBillingApplyVO finBillingApplyVO) {
        FinBillingApplyDTO finBillingApplyDTO = vo2dto(finBillingApplyVO, false);
        return new AjaxResult(HttpStatus.SUCCESS, Constants.CREATE_SUCCESS_MESSAGE, finBillingApplyService.modify(finBillingApplyDTO));
    }

    @DeleteMapping(value = "/api/fin/finbillingapply/delete/{id}", produces = MediaType.APPLICATION_JSON_VALUE)
    public AjaxResult remove(@PathVariable("id") String id) {
        return new AjaxResult(HttpStatus.SUCCESS, Constants.DELETE_SUCCESS_MESSAGE, finBillingApplyService.remove(id));
    }
    @DeleteMapping(value = "/api/fin/finbillingapply/delete", produces = MediaType.APPLICATION_JSON_VALUE)
    public AjaxResult removeBatch(@RequestParam("ids[]") List<String> ids) {
        return new AjaxResult(HttpStatus.SUCCESS, Constants.DELETE_SUCCESS_MESSAGE, finBillingApplyService.removeBatch(ids));
    }

    @GetMapping(value = "/api/fin/finbillingapply/get/{id}", produces = MediaType.APPLICATION_JSON_VALUE)
    public AjaxResult selectOneById(@PathVariable("id") String id) {
        Optional<FinBillingApplyDTO> finBillingApplyDTO = this.finBillingApplyService.selectOneById(id);
        if (finBillingApplyDTO.isPresent()) {
            return new AjaxResult(HttpStatus.SUCCESS, Constants.QUERY_SUCCESS_MESSAGE, dto2vo(finBillingApplyDTO.get()));
        } else {
            return new AjaxResult(HttpStatus.SUCCESS, Constants.QUERY_SUCCESS_MESSAGE, null);
        }

    }

    @GetMapping(value = "/api/fin/finbillingapply/list/all", produces = MediaType.APPLICATION_JSON_VALUE)
    public AjaxResult selectAll() {
        List<FinBillingApplyVO> vos = finBillingApplyService.selectAll().stream().map(t -> dto2vo(t)).collect(Collectors.toList());
        PageInfo<List<FinBillingApplyVO>> pageInfo = new PageInfo<>(vos.size(), vos);
        return new AjaxResult(HttpStatus.SUCCESS, Constants.QUERY_SUCCESS_MESSAGE, pageInfo);
    }

    @GetMapping(value = "/api/fin/finbillingapply/list/all/page", produces = MediaType.APPLICATION_JSON_VALUE)
    public AjaxResult selectPageAll(
            @RequestParam("rows") Integer limit,
            @RequestParam("page") Integer curr) {
        PageData<List<FinBillingApplyDTO>> pageData = finBillingApplyService.selectAll(limit, curr);
        List<FinBillingApplyVO> vos = pageData.getItems().stream().map(t -> dto2vo(t)).collect(Collectors.toList());
        PageInfo<List<FinBillingApplyVO>> pageInfo = new PageInfo<>(limit, pageData.getTotal(), vos);
        return new AjaxResult(HttpStatus.SUCCESS, Constants.QUERY_SUCCESS_MESSAGE, pageInfo);
    }

    @GetMapping(value = "/api/fin/finbillingapply/list/condition", produces = MediaType.APPLICATION_JSON_VALUE)
    public AjaxResult selectList(FinBillingApplyVO finBillingApplyVO) {
        FinBillingApplyDTO finBillingApplyDTO = vo2dto(finBillingApplyVO, false);
        List<FinBillingApplyVO> vos = this.finBillingApplyService.selectList(finBillingApplyDTO).stream().map(t -> dto2vo(t)).collect(Collectors.toList());
        PageInfo<List<FinBillingApplyVO>> pageInfo = new PageInfo<>(vos.size(), vos);
        return new AjaxResult(HttpStatus.SUCCESS, Constants.QUERY_SUCCESS_MESSAGE, pageInfo);
    }

    @GetMapping(value = "/api/fin/finbillingapply/list/condition/page", produces = MediaType.APPLICATION_JSON_VALUE)
    public AjaxResult selectListPage(FinBillingApplyVO finBillingApplyVO) {
        FinBillingApplyDTO finBillingApplyDTO = vo2dto(finBillingApplyVO, false);
        Integer limit = finBillingApplyVO.getRows();
        Integer curr = finBillingApplyVO.getPage();
        PageData<List<FinBillingApplyDTO>> pageData = finBillingApplyService.selectList(finBillingApplyDTO, limit, curr);
        List<FinBillingApplyVO> vos = pageData.getItems().stream().map(t -> dto2vo(t)).collect(Collectors.toList());
        PageInfo<List<FinBillingApplyVO>> pageInfo = new PageInfo<>(limit, pageData.getTotal(), vos);
        return new AjaxResult(HttpStatus.SUCCESS, Constants.QUERY_SUCCESS_MESSAGE, pageInfo);
    }

    @PostMapping(value = "/api/fin/finbillingapply/list/combox", produces = MediaType.APPLICATION_JSON_VALUE)
    public AjaxResult selectComboxList(@RequestBody FinBillingApplyVO finBillingApplyVO) {
        FinBillingApplyDTO finBillingApplyDTO = vo2dto(finBillingApplyVO, false);
        Integer limit = finBillingApplyVO.getRows();
        Integer curr = finBillingApplyVO.getPage();
        PageData<List<FinBillingApplyDTO>> pageData = finBillingApplyService.selectComboxList(finBillingApplyDTO, limit, curr);
        List<FinBillingApplyVO> vos = pageData.getItems().stream().map(t -> dto2vo(t)).collect(Collectors.toList());
        PageInfo<List<FinBillingApplyVO>> pageInfo = new PageInfo<>(limit, pageData.getTotal(), vos);
        return new AjaxResult(HttpStatus.SUCCESS, Constants.QUERY_SUCCESS_MESSAGE, pageInfo);
    }

    private FinBillingApplyVO dto2vo(FinBillingApplyDTO finBillingApplyDTO) {
        return FinBillingApplyVO.builder()
                .remark(finBillingApplyDTO.getRemark())
                .objuid(finBillingApplyDTO.getObjuid())
                .modifyUserUid(finBillingApplyDTO.getModifyUserUid())
                .modifyTime(finBillingApplyDTO.getModifyTime())
                .invoiceState(finBillingApplyDTO.getInvoiceState())
                .factoryCode(finBillingApplyDTO.getFactoryCode())
                .deleteFlag(finBillingApplyDTO.getDeleteFlag())
                .createUserUid(finBillingApplyDTO.getCreateUserUid())
                .createTime(finBillingApplyDTO.getCreateTime())
                .contractCode(finBillingApplyDTO.getContractCode())
                .billingCode(finBillingApplyDTO.getBillingCode())
                .amount(finBillingApplyDTO.getAmount())
                .build();
    }

    private FinBillingApplyDTO vo2dto(FinBillingApplyVO finBillingApplyVO, boolean newId) {
        return FinBillingApplyDTO.builder()
                .amount(finBillingApplyVO.getAmount())
                .billingCode(finBillingApplyVO.getBillingCode())
                .contractCode(finBillingApplyVO.getContractCode())
                .createTime(finBillingApplyVO.getCreateTime())
                .createUserUid(SecurityUtils.getUserId())
                .deleteFlag(finBillingApplyVO.getDeleteFlag())
                .factoryCode(finBillingApplyVO.getFactoryCode())
                .invoiceState(finBillingApplyVO.getInvoiceState())
                .modifyTime(finBillingApplyVO.getModifyTime())
                .modifyUserUid(SecurityUtils.getUserId())
                .objuid(newId ? CommonUtils.fastUUID() : finBillingApplyVO.getObjuid())
                .remark(finBillingApplyVO.getRemark())
                .build();
    }
}
